<?php

//include_once( TOptions::
// select muid, number, heat_source_direction, aspid_state from heat_points_layer where heat_source_id = 128888306130;

class TAppHeatSource
{
    // __construct
    function __construct( $aMUID )
    {
        $this->muid = $aMUID;
    }
    
    // writeData
    function writeOwnData() 
    {
        $db  = GetDbObject();
        $sql = 'select muid, number, aspid_state, aspid_data from moek_layers.heat_sources_layer where muid = '.$this->muid;
        //echo $sql;
        
        $data = $db->getSQLResultAsArray( $sql );
        
        $row = $data[0];
        
        $muid        = $row[ 'muid' ];
        $number      = mysql_escape_string( $row[ 'number' ] );
        $state       = mysql_escape_string( $row[ 'aspid_state' ] );
        $aspidData   = ( !empty( $row[ 'aspid_data' ] ) ? $row[ 'aspid_data' ] : '[]' );
        
        $rts = sprintf( '{m:"%s",n:"%s",s:"%s",p:%s}'."\n", $muid, $number, $state, $aspidData );

        $fp = fopen( TOptions::dbCacheFile( 'mnemoschema/'.$this->muid.'_s.js' ), 'w+' );
        fwrite( $fp, $rts );
        fclose( $fp );
    }
    
    // writeHeatPointsData
    function writeHeatPointsData()
    {
        $db  = GetDbObject();
        $sql = 'select muid, number, heat_source_direction, aspid_state, aspid_data from moek_layers.heat_points_layer where heat_source_id = '.$this->muid." order by number asc";
        //echo $sql;
        
        $data = $db->getSQLResultAsArray( $sql );

//echo "<pre>";
//var_dump( $data );

        $left  = '';
        $right = '';

        $lc = '';
        $rc = '';

        for ( $i = 0, $n = count( $data ); $i < $n; $i++ )
        {
            $row = $data[$i];
            
            $muid        = $row[ 'muid' ];
            $number      = mysql_escape_string( $row[ 'number' ] );
            $direction   = mysql_escape_string( $row[ 'heat_source_direction' ] );
            $state       = mysql_escape_string( $row[ 'aspid_state' ] );
            $aspidData   = ( !empty( $row[ 'aspid_data' ] ) ? $row[ 'aspid_data' ] : '[]' );
            
            $ctp = sprintf( '{m:"%s",n:"%s",d:"%s",s:"%s",p:%s}'."\n", $muid, $number, $direction, $state, $aspidData );
            
            if ( $direction == '2' ) 
            {
                $left .= $lc.$ctp;
                $lc = ',';
            }
            else 
            {
                $right .= $rc.$ctp;
                $rc = ',';
            }
        }

        $fp = fopen( TOptions::dbCacheFile( 'mnemoschema/'.$this->muid.'_2.js' ), 'w+' );
        fwrite( $fp, $left );
        fclose( $fp );

        $fp = fopen( TOptions::dbCacheFile( 'mnemoschema/'.$this->muid.'_3.js' ), 'w+' );
        fwrite( $fp, $right );
        fclose( $fp );
    }
}

?>